package net.jxta.myjxta.plugins.example;

import net.jxta.myjxta.plugin.*;
import net.jxta.myjxta.util.Group;
import net.jxta.myjxta.util.Resources;
import net.jxta.myjxta.util.objectmodel.GroupNode;
import net.jxta.myjxta.util.objectmodel.JxtaNode;
import net.jxta.myjxta.util.objectmodel.PeerNode;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Logger;

public final class SimpleExamplePlugin extends PluginBase implements Plugin, IPluginNotificationHandler, PluginContainer.IPopupProvider {

    private static final Logger LOG = Logger.getLogger(SimpleExamplePlugin.class.getName());
//    HTTPServer m_httpServer=null;


    private final ArrayList<Group> m_joinedGroups = new ArrayList<Group>();


    public SimpleExamplePlugin() {
        SimpleExamplePlugin.LOG.log(java.util.logging.Level.FINE,
                "SimpleExamplePlugin Constructor called");
    }


    public void start() {
        super.start();

        //we want a simple test menu entry on the peer nodes
        m_container.registerPopupProvider(this);

        //start can be called multiple times.. maybe we already have some joined groups... start listeners for em
        for (Iterator<Group> iterator = m_joinedGroups.iterator(); iterator.hasNext();) {
            Group group = iterator.next();
            startListenerForGroup(group);
        }
    }

    public void stop() {
        m_container.removePopupProvider(this);
        for (Iterator<Group> iterator = m_joinedGroups.iterator(); iterator.hasNext();) {
            Group group = iterator.next();
            stopListenerForGroup(group);
        }
        super.stop();
    }

    public void destroy() {
        if (m_running) {
            stop();
        }
        super.destroy();
    }

    public String getName() {
        return "SimpleExample Plugin";
    }

    // END OF PLUGIN API

    // HELPER METHODS

    private void startListenerForGroup(Group p_group) {
        //register your pipe listeners here

    }

    private void stopListenerForGroup(Group p_group) {
        //close your pipe listeners here
    }

    public IPluginNotificationHandler getPluginNotificationHander() {
        return this;  //for now we will implement this interface ourselve, it it will grow so we cant stay here...
    }

    public void groupJoined(Group p_group) {
        if (!p_group.isVisible())
            return;

        m_joinedGroups.add(p_group);

        if (isRunning()) {
            startListenerForGroup(p_group);
        }
    }

    public void groupResigned(Group p_group) {
        if (!p_group.isVisible())
            return;

        m_joinedGroups.remove(p_group);
        if (isRunning()) {
            stopListenerForGroup(p_group);
        }
    }

    //this may be fired multiple times, for example if we change from rdv to peer node
    public void groupStateChanged(Group p_group) {
        if (!p_group.isVisible())
            return;

        if (!isRunning()) { // if we are not active we are not interested in connection events
            return;
        }


        if (p_group.isConnected()) {
            //we are connected
            if (p_group.isOwnPeerRdv()) {
                //we are a Rendeszvous
            } else {
                //we are a simple peer
            }
        } else {
            //we lost the connection to the group (so we are not a rdv and we dont have a rdv right now
        }
    }

    public void popupRequested(PluginContainer.IPopupGenerator popupGenerator, ISelectableNode[] selectedNodes, MouseEvent triggerEvent) {
        if (selectedNodes != null && selectedNodes.length >= 1) {
            JxtaNode jxtaNode = selectedNodes[0].getJxtaNode();
            if (jxtaNode instanceof PeerNode) {
                PeerNode peerNode = (PeerNode) jxtaNode;
                PluginContainer.MenuPath peerPath = new PluginContainer.MenuPath(Resources.getStrings().getString("menu.peer"), KeyEvent.VK_P);
                popupGenerator.addPopup(new PluginContainer.MenuPath[]{peerPath}, 9, new ExampleAction("Show CommandPipe", "The PipeID of " + peerNode.getPeer().getName() + "�s command pipe is " + peerNode.getPeer().getPipeAdvertisement().getID()));
            } else if (jxtaNode instanceof GroupNode) {
                //warning!! group node is not working yet... the tree doesnt use the new context-menu (yet)
                //this code here is only for reference....
                Group g = ((GroupNode) jxtaNode).getGroup();
                PluginContainer.MenuPath groupPath = new PluginContainer.MenuPath(Resources.getStrings().getString("menu.group"), KeyEvent.VK_G);
                popupGenerator.addPopup(new PluginContainer.MenuPath[]{groupPath}, 9, new ExampleAction("Example on GroupNode", "Group is " + g.getId()));
            }
        }
    }

    private class ExampleAction extends AbstractAction {
        private final String message;

        public ExampleAction(String name, String p_message) {
            super(name);
            message = p_message;
        }

        public void actionPerformed(ActionEvent e) {
            m_container.getMyJxta().getView().showMessageDialog(message);
        }
    }
}
